#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define maxlen 30

/* Koder - archaeological description tool
 * (c) Milosz Piglas 2007 
 * Distribution under BSD-like license.
 * It is distributed with absolutely no warranties
 */

struct record
{
	char *NAME;
	char *VAL;
};

void ropen(char *fname,char *outtab[], int *i)
{
	char gc;
	char line[maxlen], *mem;
	FILE *ro;
	int cc=0; //licznik znakow
	int wc=0; //licznik slow
	ro = fopen(fname,"r");
	while ((gc = getc(ro)) != EOF)
	{
		if (gc != '\n')
		{
			line[cc] = gc;
			cc++;
		}
		else
		{
			line[cc] = '\0';
			mem = malloc(cc);
			strcpy(mem,line);
			outtab[wc] = mem;
			wc++;
			cc = 0;

		}
	}
	fclose(ro);
	*i = wc;
}


void records_tab(char *from[], struct record *records_t, int len )
{
	/* wypelnia tablice struktur */

	int  kc=0, cc=0, i;
	char l, *memn, *memc;
	char *line;
	char name[maxlen];
	char value[maxlen];
	for (i=0;i<len;i++)
	{

		line = malloc(strlen(from[i]));
	        strcpy(line,from[i]);
		while ((l = *(line+cc)) != ':' && cc < 30)	
		{
			name[cc] = l;
			cc++;
		}
		name[cc] = '\0';
		memn = malloc(cc);
		strcpy(memn,name);
                (records_t+i)->NAME = memn;
		cc++;
		kc = cc;
		while ((l = *(line+cc)) != '\0' && cc-kc < 30)
		{
			value[cc-kc] = l;
			cc++;
		}
		value[cc-kc] = '\0';
		memc = malloc(cc-kc);
		strcpy(memc,value);
		(records_t+i)->VAL = memc;
		cc =0;
	}
}	

void keylistshow(struct record *records_t, int len)
{
	int i;
	for (i=0;i<len;i++)
		printf("%s : %s\n",(records_t+i)->NAME,(records_t+i)->VAL);
}


int iadd(char *items_t[], struct record *records_t, int len, int bco)
{
	char entry[30], *tmpcode, *mem,*cos;
	int x, wc=bco;
	int close = 0;
        int lentmp=1;
	printf("To finish code creating type '.end.' >>\n");
	printf("To save created codes type '.close.'\n");
	printf("To see keylist type '.list.'\n");
	while (close == 0 && wc < 200) {
		printf(">> ");
		scanf("%s",&entry[0]);
		if (strcmp(entry,".end.") != 0 && strcmp(entry,".close.") != 0 && strcmp(entry,".list.") && wc < 200)
		{
			if (lentmp == 1)
			{
				tmpcode = malloc(1);
				*(tmpcode) = '\0';
			}
			for (x=0;x<len;x++)
				//printf("nazwa: %s",(strtb+x)->name);
				if (strcmp((records_t+x)->NAME,entry) == 0)
				{       lentmp+=strlen(entry);
					tmpcode = realloc(tmpcode,lentmp);
					strcpy(tmpcode,strcat(tmpcode,(records_t+x)->VAL));
					printf("%s\n",tmpcode);
				}
		}
		else
		{
			if (strcmp(entry,".end.") == 0 && lentmp > 1)
			{
				items_t[wc] = tmpcode;
				//free(tmpcode);
				printf("code: %s\n",tmpcode);
				lentmp = 1;
				wc++;
			}
			if (strcmp(entry,".close.") == 0)
				close = 1;
			if (strcmp(entry,".list.") == 0)
				keylistshow(records_t,len);
			if (wc == 200)
				printf("I'm sorry. You can't add any more codes!");
		}
	}
	return wc;

}

void clean(char *ptab[], int len)
{
	int i;
	for (i=0;i<len;i++)
		free(ptab[i]);
}

void rsave(char *fname, char *items_t[], int tlen)
{
	FILE *sf;
	int i;
	sf = fopen(fname,"w");
	for (i=0;i<tlen;i++)
	{
		fputs(items_t[i],sf);
	        putc('\n',sf);
	}
	fclose(sf);
}

void iremove(char *items_t[], int *tlen)
{
	int i, poz;
	for (i=0;i<*tlen;i++)
	{
		printf("%d - %s\n",i+1,items_t[i]);
	}
	scanf("%d",&poz);
	strcpy(items_t[poz-1],items_t[*tlen-1]);
	free(items_t[*tlen-1]);
	*tlen = *tlen-1;
}







